import { _decorator, Component, Node, tween } from 'cc'
const { ccclass, property } = _decorator

@ccclass('SheepMove')
export class SheepMove extends Component {
  // 移动路径点
  @property([Node])
  movePointsTrans: Node[] = []
  start() {
    if (!!this.movePointsTrans.length) {
      this.moveAlongPath()
    }
  }
  moveAlongPath() {
    const points = this.movePointsTrans.map(item => item.position)
    let currentIndex = 0
    const moveToNextPoint = () => {
      const targetPoint = points[currentIndex]
      currentIndex = (currentIndex + 1) % points.length
      // 创建新的tween动画，移动到目标点
      tween(this.node)
        .to(1, { position: targetPoint }, { easing: 'sineInOut' })
        .call(moveToNextPoint)
        .start()
    }
    moveToNextPoint()
  }

  update(deltaTime: number) {
    if (this.node.position.y >= 0) {
      this.node.setRotationFromEuler(0, 180, 0)
    } else {
      this.node.setRotationFromEuler(0, 0, 0)
    }
  }
}
